<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Models\Course;
use App\Services\CourseService;
use Illuminate\Http\Request;

class CourseController extends Controller
{

    protected $CourseService;
    public function __construct(CourseService $CourseService)
    {
        $this->CourseService = $CourseService;
    }

    // /**
    //  * Display a listing of the resource.
    //  *
    //  * @return \Illuminate\Http\Response
    //  */
    // public function index(Request $request)
    // {
    //     $title = $request->title;
    //     $pageSize = $request->pageSize ?: 10;
    //     $page = $request->page ?: 1;
    //     $skip = $page * $pageSize - $pageSize;

    //     $sql = $this->CourseService->get($title);
    //     $sql->select('id','title','cover','type_id','price','original_price','sort','clicks','status','shop_type','tag');
    //     $total = $sql->count();
    //     $data = $sql->skip($skip)->take($pageSize)->orderBy('created_at','desc')->get();
    //     return response()->json(['code' => 200, 'message' => '操作成功','page'=>$page,'pageSize'=>$pageSize,'total'=>$total,'data'=>$data]);
    // }


    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $title = $request->title;
        $pageSize = $request->pageSize ?: 10;
        $page = $request->page ?: 1;
        $skip = $page * $pageSize - $pageSize;

        $sql = $this->CourseService->get($title);
        $sql->select('id','title','cover','type_id','price','original_price','sort','clicks','status','shop_type','tag');
        $total = $sql->count();
        $data = $sql->skip($skip)->take($pageSize)->orderBy('created_at','desc')->get();
        return response()->json(['code' => 200, 'message' => '操作成功','page'=>$page,'pageSize'=>$pageSize,'total'=>$total,'data'=>$data]);
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $POSTONLY = Course::POSTONLY;
        $input = $request->only($POSTONLY);
        $videos = $request->videos ? json_decode($request->videos) : '';
        $data = $this->CourseService->insert($input,$videos);
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request)
    {
        $data = Course::find($request->id);
        $videos = $data->videos;
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data,'videos'=>$videos]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $POSTONLY = Course::POSTONLY;
        $input = $request->only($POSTONLY);
        $input['id'] = $request->id;
        $videos = $request->videos ? json_decode($request->videos) : '';
        $data = $this->CourseService->update($input,$videos);
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function delete($id)
    {
        if(!$id){
            return response()->json(['code' => 404, 'message' => '请选择要操作的记录']);
        }
        $data = Course::find($id);
        $data->delete();
        return response()->json(['code' => 200, 'message' => '操作成功']);
    }
}
